
require 'View/UIBase/UIBase'

UISevenRankTips = UIBase.New(PanelResNames.UISevenRankTips)

function UISevenRankTips.Init(func, type)
    UISevenRankTips.curType = type;
    UIBase.Init(UISevenRankTips,func);
end

-- 创建回调
function UISevenRankTips:OnCreate()
    self.RewardList = self:SetDynamicList(self:GetChild("RewardList"),self.setData)
    self:GetChild("Name").text = getLanguage("RaceRankDes16");
    self:addEvent();
end

-- 显示
function UISevenRankTips:OnEnable()
    if self.curType == nil then
        self:OnClose();
        return ;
    end
    self:Refresh();
end

function UISevenRankTips:addEvent()
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
end

function UISevenRankTips:removeEvent()
    self.RewardList.scrollPane.onScrollEnd:Clear();
end

function UISevenRankTips:Refresh()
    self.curDay, self.nowCountDown, self.round = logicMgr.NoticeManager.getSevenRankDay();
    if self.curDay == nil then
        self:OnClose();
        return;
    end
    self.nowTypeTableData = logicMgr.NoticeManager.getSevenRankTableDataByType(self.curType);
    self.subRankTableDataList = logicMgr.NoticeManager.getSevenRankSubReward(self.curType);
    table.sort(self.subRankTableDataList, function (a, b)
        return a.f_RaceRankId < b.f_RaceRankId;
    end);

    local temp = os.date("*t", math.floor(HelperFunc.GetServerTime()));
    local serverHour = tonumber(temp.hour);
    local stageTime = logicMgr.NoticeManager.getStageRewardTime();
    self.rewardIndex = 1;
    if serverHour >= stageTime[4] or serverHour < stageTime[1] then
        self.rewardIndex = 1;
    elseif serverHour < stageTime[2] then
        self.rewardIndex = 2;
    elseif serverHour < stageTime[3] then
        self.rewardIndex = 3;
    else
        self.rewardIndex = 4;
    end

    self.RewardList.numItems = #self.subRankTableDataList;
end

function UISevenRankTips:setData(index, itemObj)
    local title = itemObj:GetChild("AwardName");
    local listReward = itemObj:GetChild("listReward");
    local cfg = self.subRankTableDataList[index];
    local rankCfg = cfg.f_Rank;
    if type(rankCfg) ~= "table" then
        rankCfg = decodeJsonStrToTabel(rankCfg)
    end
    if rankCfg[1] == rankCfg[2] then
        title.text = string.format(getLanguage("RaceRankDes14"), rankCfg[1]);
    else
        title.text = string.format(getLanguage("RaceRankDes15"), rankCfg[1], rankCfg[2]);
    end
    local reward;
    if self.nowTypeTableData.f_Day ~= self.curDay or self.rewardIndex == 4 then
        reward = cfg.f_Award;
    else
        if self.rewardIndex == 1 then
            reward = cfg.f_Award1;
        elseif self.rewardIndex == 2 then
            reward = cfg.f_Award2;
        else
            reward = cfg.f_Award3;
        end
    end
    logicMgr.CostManager.RefreshItemList(listReward, reward, 4);
end

-- 关闭
function UISevenRankTips:OnClose()
    self:OnBack();
end

-- 隐藏
function UISevenRankTips:OnDisable()
end

-- 卸载
function UISevenRankTips:OnDestroy()
    self:removeEvent();
end

return UISevenRankTips